package com.llynosy.interview.mapper;

import com.llynosy.interview.po.Problem;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

public interface ProblemMapper extends Mapper<Problem>{

    @Select("select p.* from tb_problem p left join tb_problem_tag  pt on p.id=pt.problemId where pt.tagId=#{tagId}")
    List<Problem> queryProblemByTag(@Param("tagId")Long tagId);

    @Select("select * from tb_problem order by likeNumber desc limit 20")
    List<Problem> queryHotProblem();

    @Select("select * from tb_problem order by createTime desc limit 20")
    public List<Problem> queryLatestProblem();

    @Select("select p.* from tb_problem p left join tb_problem_tag  pt on p.id=pt.problemId where pt.tagId=#{tagId}  order by likeNumber desc limit 15")
    public List<Problem> queryHotProblemBelowATag(@Param("tagId")Long tagId);

    @Select(" select o_tp.* from tb_problem_tag o_tpt inner join \n" +
            "(select pt.problemId,pt.tagId from tb_problem p inner join tb_problem_tag pt on p.id=pt.problemId \n" +
            " where p.id=#{problemId}) m_tb on o_tpt.tagId=m_tb.tagId and o_tpt.problemId!=m_tb.problemId inner join \n" +
            " tb_problem o_tp on o_tpt.problemId=o_tp.id limit 15" )
    public List<Problem> queryProblemRelatedProblems(@Param("problemId")Long problemId);

    /**
     * 计算某个标签下问题数量
     *
     * @param tagId
     * @return
     */
    @Select("select count(1) from tb_problem p left join tb_problem_tag  pt on p.id=pt.problemId where pt.tagId=#{tagId}")
    public Integer countNumberOfProblemsUnderTag(@Param("tagId")Long tagId);

}
